import gameCtr from "../ctrs/gameCtr";
import { trackTarget } from "../libs/core/utils";
import BaseEle, { campType, soliderType } from "./baseEle";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-29 10:51:21
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-07 11:05:49
 */
const { ccclass, property } = cc._decorator;

// 重力
const G = -940; // -980 越高 抛的越高
// 固定速度
const V = 1000;

@ccclass
export default class arrowEle extends cc.Component {

  @property({ type: cc.Sprite, tooltip: "贴图" })
  sp: cc.Sprite = null;

  //目标点
  public targetPos: cc.Vec2 = null;

  public startPos: cc.Vec2 = null;

  public aoeNum: number = 5;

  public masterType: soliderType = soliderType.cannon;

  // 平抛线性初速度
  private _linearVelocity_1: cc.Vec2 = cc.v2(0, 0);
  // 高抛线性初速度
  private _linearVelocity_2: cc.Vec2 = cc.v2(0, 0);

  private _count: number = 0;

  private num: number = 0;

  private campType: campType;

  public setTargetPos(startPos, targetPos, aoeNum, type, campType) {
    this.targetPos = targetPos;
    this.startPos = startPos;
    this.aoeNum = aoeNum;
    this.masterType = type;
    this.campType = campType;
    this.num = 0;
    this.highThrowMove();
  }

  public isCollide() {
    return Math.sqrt(Math.pow(this.node.x - this.targetPos.x, 2) + Math.pow(this.node.y - this.targetPos.y, 2)) < 10;
  }


  /**
   * 范围伤害
   */
  public overHurt() {

    let childs = gameCtr.ins.eleNode.children;

    for (let i: number = 0; i < childs.length; i++) {

      let ele: cc.Node = childs[i];
      // console.log("hurtBlood");
      if (!ele) {
        // console.log("hurtBlood");
        continue;
      }

      if (this.num >= this.aoeNum) break;

      let spr = ele.getComponent(BaseEle);

      if (spr.campType != this.campType && !spr.isFly && !spr.isFire && this.node.getBoundingBox().intersects(ele.getBoundingBox())) {

        let dir = this.node.x > ele.x ? 1 : -1;

        this.num++;

        // ele.getComponent(BaseEle).doFly(dir, 3);
        ele.getComponent(BaseEle).hurtBlood(1, this.campType);
      }
    }
  }

  public move(dt) {

    if (this.isCollide()) {
      // this.overHurt();
      this.recover();
    } else {
      // this.trackTargetMove();
      this.doHighThrowMove();
    }
  }

  /**
 * 高抛路径
 */
  public highThrowMove() {
    let targetX: number;

    let targetY: number

    if (!this.targetPos) return;

    targetX = this.targetPos.x;

    targetY = this.targetPos.y;

    let s = targetX - this.startPos.x;
    let h = targetY - this.startPos.y;

    let a = G * 1 * s / (2 * V * V);

    let b = 1;
    let c = a - h / s;
    let delta = b * b - 4 * a * c;

    if (delta >= 0) {
      // 一元二次方程求根公式
      let t1 = (-b + Math.sqrt(delta)) / (2 * a); // 平抛 tan 值
      let t2 = (-b - Math.sqrt(delta)) / (2 * a); // 高抛 tan 值

      // 二、三象限角度要加 180
      let alpha1 = Math.atan(t1) + (s < 0 ? Math.PI : 0);
      let alpha2 = Math.atan(t2) + (s < 0 ? Math.PI : 0);

      let v_x_1 = Math.cos(alpha1) * V;
      let v_y_1 = Math.sin(alpha1) * V;
      let v_x_2 = Math.cos(alpha2) * V;
      let v_y_2 = Math.sin(alpha2) * V;

      this._linearVelocity_1.x = v_x_1;
      this._linearVelocity_1.y = v_y_1;

      this._linearVelocity_2.x = v_x_2;
      this._linearVelocity_2.y = v_y_2;
    } else {
      this._linearVelocity_1 = cc.Vec2.ZERO;
      this._linearVelocity_2 = cc.Vec2.ZERO;
    }

    // this.drawArrowTrace();
  }

  private preX: number = 0;

  private preY: number = 0;

  private doHighThrowMove() {
    this._count++;
    let linearVelocity = this._linearVelocity_1;
    let dt = 0.016;
    let time = dt * this._count;
    // s = v_x * t
    let dx = linearVelocity.x * time;
    // h = (v_y + 0.5 * a * t) * t
    let dy = linearVelocity.y * time + 0.5 * G * 1 * time * time;
    // 当前时间点坐标

    this.node.setPosition(cc.v2(this.startPos.x + dx, this.startPos.y + dy));
    let angle = cc.v2(this.startPos.x + dx - this.preX, this.startPos.y + dy - this.preY).signAngle(cc.v2(1, 0));

    this.node.angle = -angle * 180 / Math.PI;

    this.preX = this.startPos.x + dx;
    this.preY = this.startPos.y + dy;

  }

  /**
   * 面向目标移动
   */
  public trackTargetMove() {

    if (!this.targetPos) return;

    let speedArr = trackTarget(this.targetPos.x, this.targetPos.y, this.startPos.x, this.startPos.y, 15);

    this.node.x += speedArr[0];

    this.node.y += speedArr[1];

    this.node.angle = speedArr[2] * (180 / Math.PI);
  }

  public recover() {

    this.overHurt();

    this._count = 0;

    this._linearVelocity_1 = cc.v2(0, 0);

    this._linearVelocity_2 = cc.v2(0, 0);

    gameCtr.ins.poolObj['arrow'].put(this.node);
  }
}
